package com.aierkeji.b2c.utils;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileUtils extends org.apache.commons.io.FileUtils {
	
	private static final Logger log = LoggerFactory.getLogger(FileUtils.class);

	//不适用大文件，会造成内存溢出
	public static String readStringNIO(String path){
		try {
			return new String(Files.readAllBytes(Paths.get(path)));
		} catch (IOException e) {
			log.error("FileUtils readStringNIO exception:",e);
			return null;
		}
	}


	
	public static String getExtension(String fileName){
		String extension = StringUtils.substring(fileName, 
				StringUtils.lastIndexOf(fileName, ".")+1, fileName.length());
		return extension;
	}
	
	public static boolean checkFileExtension(String fileName,String type){
		if(StringUtils.isNotBlank(fileName) &&
		   StringUtils.isNotBlank(type) &&
		   StringUtils.indexOf(fileName, ".") >= 0){
			String extension = StringUtils.substring(fileName, 
					StringUtils.lastIndexOf(fileName, ".")+1, fileName.length());
			if(StringUtils.equalsIgnoreCase("excel", type)){
				if(StringUtils.equalsIgnoreCase(extension, "xls") ||
				   StringUtils.equalsIgnoreCase(extension, "xlsx")){
					return true;
				}
			}else if(StringUtils.equalsIgnoreCase("excel-2003", type)){
				if(StringUtils.equalsIgnoreCase(extension, "xls")){
					return true;
				}
			}else if(StringUtils.equalsIgnoreCase("img", type)){
				if(StringUtils.equalsIgnoreCase(extension, "jpg") ||
				   StringUtils.equalsIgnoreCase(extension, "bmp") ||
				   StringUtils.equalsIgnoreCase(extension, "png") ||
				   StringUtils.equalsIgnoreCase(extension, "jpeg") ||
				   StringUtils.equalsIgnoreCase(extension, "gif")){
					return true;
				}
			}else{
				if(StringUtils.equalsIgnoreCase(extension, type)){
					return true;
				}
			}
		}
		return false;
	}
	
	public static void writeToFile(InputStream inputStream, File file) 
			throws IOException {
		if(file.exists()){
			file.delete();
		}
		log.info("writeToFile:"+file.getAbsolutePath());
    	file.createNewFile();
        int read = 0;
        byte[] bytes = new byte[1024];
        OutputStream out = new FileOutputStream(file);
        while ((read = inputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
    }
	
	

	//把文件打包压缩
	public static void getCompressionFile(List<File> fileList, String savePath, String fileName) throws IOException{
		FileOutputStream fileOutputStream = null;// 创建 FileOutputStream 对象
		ZipOutputStream zipOutputStream = null;// 创建 ZipOutputStream
		FileInputStream fileInputStream = null;// 创建 FileInputStream 对象
		List<String> fileNameList = new ArrayList<String>();
		try{
			fileOutputStream = new FileOutputStream(savePath + fileName);// 实例化 FileOutputStream 对象
			zipOutputStream = new ZipOutputStream(fileOutputStream);// 实例化 ZipOutputStream 对象
			ZipEntry zipEntry = null;// 创建 ZipEntry 对象
			for (int i = 0; i < fileList.size(); i++) {// 遍历源文件数组
				fileInputStream = new FileInputStream(fileList.get(i));// 将源文件数组中的当前文件读入 FileInputStream 流中
				if(!fileNameList.contains(fileList.get(i).getName())){
					zipEntry = new ZipEntry(fileList.get(i).getName());// 实例化 ZipEntry 对象，源文件数组中的当前文件
				}else{
					String startStr = fileList.get(i).getName().substring(0, fileList.get(i).getName().lastIndexOf("."));
					String endStr = fileList.get(i).getName().substring(fileList.get(i).getName().lastIndexOf("."));
					zipEntry = new ZipEntry(startStr + "(" + i + ")" + endStr);
				}
				fileNameList.add(fileList.get(i).getName());
				zipOutputStream.putNextEntry(zipEntry);
				int len;// 该变量记录每次真正读的字节个数
				byte[] buffer = new byte[1024];// 定义每次读取的字节数组
				while ((len = fileInputStream.read(buffer)) > 0) {
					zipOutputStream.write(buffer, 0, len);
				}
			}
		}catch (IOException e) {
			throw new IOException(e);
		}finally{
			if(zipOutputStream != null){
				zipOutputStream.closeEntry();
			}
			if(zipOutputStream != null){
				zipOutputStream.close();
			}
			if(fileInputStream != null){
				fileInputStream.close();
			}
			if(fileOutputStream != null){
				fileOutputStream.close();
			}
		}
	}
}
